package com.newture.lxz.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newture.lxz.constant.Constant;
import com.newture.lxz.db.mapper.LdCourtAddressMapper;
import com.newture.lxz.db.model.LdCourtAddress;
import com.newture.lxz.db.model.LdCourtAddressExample;
import com.newture.lxz.db.model.LdCourtAddressWithBLOBs;
import com.newture.lxz.errors.AppException;
import com.newture.lxz.errors.Errors;
import com.newture.lxz.pvg.Pvg;
import com.newture.lxz.services.ICourtAddressSrv;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author xiao.hu
 * @date 2021-09-25
 * @apiNote
 */
@Service("CourtAddressSrv")
public class CourtAddressSrv implements ICourtAddressSrv {

    @Resource
    LdCourtAddressMapper ldCourtAddressMapper;

    /**
     * 分页查询
     * @param pageNumber
     * @param name
     * @param expectColumns
     * @return
     */
    @Override
    public PageInfo<LdCourtAddress> query(int pageNumber, int pageSize, String name, String[] expectColumns) {
        PageHelper.startPage(pageNumber, pageSize);

        LdCourtAddressExample example = new LdCourtAddressExample();
        LdCourtAddressExample.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotBlank(name)){
            criteria.andCourtNameLike("%" + name + "%");
        }
        criteria.andIsDeletedEqualTo(Constant.NO);

        example.setDynamicColumns(expectColumns);

        List<LdCourtAddress> list = ldCourtAddressMapper.selectByExample(example);
        PageInfo<LdCourtAddress> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public LdCourtAddress findById(int id, String[] columns) {
        LdCourtAddressExample example = new LdCourtAddressExample();
        LdCourtAddressExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andIsDeletedEqualTo(Constant.NO);

        example.setDynamicColumns(columns);
        List<LdCourtAddress> list = ldCourtAddressMapper.selectByExample(example);
        if(list.isEmpty()) {
            throw new AppException(Errors.NO_RECORD_FOUND_BY_ID.name(),
                    String.format("ID is %s", id), "No record found in CourtAddressSrv.findById()");
        }

        if(list.size() > 1) {
            throw new AppException(Errors.MORE_THAN_ONE_RECORDS_FOUND.name(),
                    String.format("ID is %s", id), "More than one record found in CourtAddressSrv.findById()");
        }

        return list.get(0);
    }

    /**
     * 全字段模式新增
     * @param obj
     * @return
     */
    @Override
    public int insert(LdCourtAddressWithBLOBs obj) {
        obj.setCreator(Pvg.getPvgName());
        obj.setModifier(Pvg.getPvgName());
        obj.setGmtCreated(LocalDateTime.now());
        obj.setIsDeleted(Constant.NO);
        obj.setVersion(1);
        int effectCount = ldCourtAddressMapper.insert(obj);
        if(effectCount != 1) {
            throw new AppException(Errors.SAVE_ERR.name(), obj.toString(), "CourtAddressSrv.insert() failed");
        }

        return obj.getId();
    }


    @Override
    public int insertSelective(LdCourtAddressWithBLOBs obj) {
        obj.setCreator(Pvg.getPvgName());
        obj.setModifier(Pvg.getPvgName());
        obj.setGmtCreated(LocalDateTime.now());
        int effectCount = ldCourtAddressMapper.insertSelective(obj);
        if(effectCount != 1) {
            throw new AppException(Errors.SAVE_ERR.name(), obj.toString(), "CourtAddressSrv.insertSelective() failed");
        }

        return obj.getId();
    }

    /**
     * Spring 事务方法只支持 public
     * 测试事务回滚
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertSelective(List<LdCourtAddressWithBLOBs> list) {
        for(LdCourtAddressWithBLOBs obj : list) {
            insertSelective(obj);
        }

        throw new AppException(Errors.SAVE_ERR.name(), "", "Fake insert operation, test transaction rollback");
    }

    @Override
    public int update(int id, int version, LdCourtAddressWithBLOBs obj) {
        obj.setVersion(null);
        obj.setCreator(Pvg.getPvgName());
        obj.setModifier(Pvg.getPvgName());

        LdCourtAddressExample example = new LdCourtAddressExample();
        example.createCriteria().andIdEqualTo(id).andVersionEqualTo(version).andIsDeletedEqualTo(Constant.NO);
        int effectCount = ldCourtAddressMapper.updateByExampleSelective(obj, example);
        if(effectCount != 1) {
            throw new AppException(Errors.UPDATE_ERR.name(), obj.toString(), "CourtAddressSrv.update() failed");
        }

        return effectCount;
    }

    @Override
    public int remove(int id, int version) {
        LdCourtAddressWithBLOBs obj = new LdCourtAddressWithBLOBs();
        obj.setIsDeleted(Constant.YES);
        obj.setVersion(null);
        obj.setCreator(Pvg.getPvgName());
        obj.setModifier(Pvg.getPvgName());


        LdCourtAddressExample example = new LdCourtAddressExample();
        example.createCriteria().andIdEqualTo(id).andVersionEqualTo(version).andIsDeletedEqualTo(Constant.NO);
        int effectCount = ldCourtAddressMapper.updateByExampleSelective(obj, example);
        if(effectCount != 1) {
            throw new AppException(Errors.UPDATE_ERR.name(), obj.toString(), "CourtAddressSrv.remove() failed");
        }

        return effectCount;
    }
}
